package com.cqie.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.cqie.dto.TokenUserLoginDto;
import io.jsonwebtoken.JwtException;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtTokenUtil {

    // 创建一个秘钥
    public static final String TOKEN_KEY = "javasdfasdfasdfxixiahahahehe";

    // 设置token的有效期  15min
    public final static long KEEP_TIME = 15 * 60 * 60 * 1000;

    /**
     * 生成token
     * @param id   主键id
     * @param name 用户名
     * @return token
     */
    public static String buildJwt(Integer id , String name){
        Date date = new Date(System.currentTimeMillis() + KEEP_TIME);
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_KEY);
        // 设置头部信息
        Map header = new HashMap<>(2);
        header.put("typ","JWT");
        header.put("alg","HS256");
        return JWT.create()
                .withHeader(header)
                .withClaim("id",id)
                .withClaim("name",name)
                .withExpiresAt(date)
                .sign(algorithm);
    }


    /**
     * 校验token是否正确
     * @param token
     * @return
     */
    public static boolean verify(String token) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_KEY);
            JWTVerifier verifier = JWT.require(algorithm)
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }



    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户名
     */
    public static String getAccount(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("accountName").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }
    /**
     * 获取登陆用户ID
     * @param token
     * @return
     */
    public static String getAccountId(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("account").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }


    public static TokenUserLoginDto getDoctorDto(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);

            TokenUserLoginDto dto = new TokenUserLoginDto();
            dto.setDoctorId(jwt.getClaim("id").asInt());
            dto.setDoctorAccount(jwt.getClaim("name").asString());
//            dto.setDoctorPassword(jwt.getClaim("doctorPassword").asString());
//            dto.setDoctorPhone(jwt.getClaim("doctorPhone").asString());

            return dto;
        } catch (JWTDecodeException e) {
            // 处理解码异常，例如打印日志或抛出自定义异常
            e.printStackTrace();
            return null;
        } catch (JwtException e) {
            // 处理其他JWT异常，例如过期异常
            e.printStackTrace();
            return null;
        }
    }

    public static TokenUserLoginDto getPatientDto(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);

            TokenUserLoginDto dto = new TokenUserLoginDto();
            dto.setPatientId(jwt.getClaim("id").asInt());
            dto.setPatientAccount(jwt.getClaim("name").asString());
//            dto.setDoctorPassword(jwt.getClaim("doctorPassword").asString());
//            dto.setDoctorPhone(jwt.getClaim("doctorPhone").asString());

            return dto;
        } catch (JWTDecodeException e) {
            // 处理解码异常，例如打印日志或抛出自定义异常
            e.printStackTrace();
            return null;
        } catch (JwtException e) {
            // 处理其他JWT异常，例如过期异常
            e.printStackTrace();
            return null;
        }
    }
}